package com.springmvc.t11.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

@Component
public class InitServlet implements WebApplicationInitializer, ApplicationContextAware {

	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		ServletRegistration.Dynamic dy = servletContext.addServlet("app",getServlet());
		dy.setLoadOnStartup(1);
		dy.addMapping("/");
	}
	public  static DispatcherServlet getServlet(){
		return context.getBean(DispatcherServlet.class);
	}
}
